package com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.repository.impl;

import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.entity.EarEveManage;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.entity.JcStrongCatalog;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.entity.SimEarthquake;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.repository.SimEarthquakeRepository;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author:
 * @CreateTime: 2023/11/14 16:39
 * @Description:
 */
@Repository
public class SimEarthquakeRepositoryImpl implements SimEarthquakeRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void simEarSave(SimEarthquake simEarthquake) {
        //经纬度组成空间字段数据
        String longitude = simEarthquake.getLongitude().toString();
        String latitude = simEarthquake.getLatitude().toString();
        String geom = "POINT("+longitude+" "+latitude+")";
        simEarthquake.setGeom(geom);
        String sql="insert into xczh_simulate_earthquake(jsc_id,province,city,district,town,event_id," +
                "event_abbrevia,occurrence_date,occurrence_time,location,longitude,latitude,magnitude,depth,del_flag,create_user,create_time,update_user,update_time,geom) " +
                "values(:jscId, :province, :city, :district, :town, :eventId, :eventAbbrevia, :occurrenceDate, :occurrenceTime, " +
                ":location, :longitude, :latitude, :magnitude, :depth, :delFlag, :createUser,:createTime,:updateUser,:updateTime,st_geomfromtext(:geom,4490))";
        SqlParameterSource source = new BeanPropertySqlParameterSource(simEarthquake);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void simEarDelete(String id) {
        String sql = "update xczh_simulate_earthquake set del_flag= ? where jsc_id = ?";
        jdbcTemplate.update(sql, YNEnum.Y.toString(),id);
    }

    @Override
    public void simEarUpdate(SimEarthquake simEarthquake) {
        String sql="update xczh_simulate_earthquake set province = :province " +
                ",city = :city ,district = :district,town = :town,event_abbrevia = :eventAbbrevia,occurrence_date = :occurrenceDate " +
                ",location = :location ,longitude = :longitude,latitude = :latitude,magnitude = :magnitude,depth = :depth " +
                "where jsc_id = :jscId";
        SqlParameterSource source = new BeanPropertySqlParameterSource(simEarthquake);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public List<JcStrongCatalog> getSimEarList(String magnitude) {
        StringBuilder sql = new StringBuilder("select *,district as county from xczh_simulate_earthquake where del_flag = '0' ");
        if(PlatformObjectUtils.isNotEmpty(magnitude)){
            sql.append(" and magnitude = '"+magnitude+"'");
        }
        List<JcStrongCatalog> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(JcStrongCatalog.class));
        return list;
    }
}
